package com.iaiai.notepad;

import android.app.Activity;
import android.content.Intent;
import android.gesture.Gesture;
import android.gesture.GestureOverlayView;
import android.os.Bundle;
import android.os.Environment;
import android.text.Html;
import android.text.Spanned;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

/**
 * 
 * <p>
 * Title: NewNotepad.java
 * </p>
 * <p>
 * E-Mail: 176291935@qq.com
 * </p>
 * <p>
 * QQ: 176291935
 * </p>
 * <p>
 * Http: iaiai.iteye.com
 * </p>
 * <p>
 * Create time: 2011-12-4
 * </p>
 * 
 * @author 丸子
 * @version 0.0.1
 */
public class NewNotepadActivity extends Activity {

	protected Button btnSave;

	protected View btnReturn;
	
	protected EditText content;
	
	protected TextView title;
	
	protected View btnSx;
	
	protected GestureOverlayView overlay;
	
	protected Gesture mGesture;

	protected void init() {
		btnSave = (Button) findViewById(R.id.btnSave);
		btnReturn = findViewById(R.id.btnReturn);
		content = (EditText) findViewById(R.id.content);
		title = (TextView) findViewById(R.id.title);
		btnSx = findViewById(R.id.sx);
		overlay = (GestureOverlayView) findViewById(R.id.gestures_overlay);
		
		// GestureOverlayView.GESTURE_STROKE_TYPE_MULTIPLE 设置支持多笔划
        // GestureOverlayView.GESTURE_STROKE_TYPE_SINGLE 仅支持单一笔划
		overlay.setGestureStrokeType(GestureOverlayView.GESTURE_STROKE_TYPE_MULTIPLE);
		// 手势识别的监听器
		overlay.addOnGestureListener(new GesturesProcessor(content,mGesture));
		
		btnReturn.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				finish(); // 关闭
			}
		});
		btnSx.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if (!sdCardIsExsit()) {
					Toast.makeText(NewNotepadActivity.this, "sdcard不存在或有写保护!",
							Toast.LENGTH_LONG).show();
					return;
				}
				
				if(overlay.getVisibility()==View.VISIBLE){
					overlay.setVisibility(View.GONE);
				}else{
					overlay.setVisibility(View.VISIBLE);
				}
			}
		});
	}
	
	@Override
	protected void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);

		if (mGesture != null) {
			outState.putParcelable("gesture", mGesture);
		}
	}
	
	@Override
	protected void onRestoreInstanceState(Bundle savedInstanceState) {
		super.onRestoreInstanceState(savedInstanceState);

		mGesture = savedInstanceState.getParcelable("gesture");
		if (mGesture != null) {
			final GestureOverlayView overlay = (GestureOverlayView) findViewById(R.id.gestures_overlay);
			overlay.post(new Runnable() {
				public void run() {
					overlay.setGesture(mGesture);
				}
			});
		}
	}
	
	protected void initListener(){
		btnSave.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				Bundle bundle = new Bundle();
				bundle.putString("filename", title.getText().toString());	//文件名
				bundle.putString("content", Html.toHtml(content.getText()));	//内容
				
				Intent it = new Intent(NewNotepadActivity.this, FolderActivity.class);
				it.putExtras(bundle);
				startActivity(it);
			}
		});
	}
	
	protected void setView(){
		setContentView(R.layout.new_notepad);
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setView();

		init();
		initListener();
	}
	
	/**
	 * 判断sdcard是否已安装
	 * 
	 * @return true安装，false未安装
	 */
	private boolean sdCardIsExsit() {
		return Environment.getExternalStorageState().equals(
				Environment.MEDIA_MOUNTED);
	}

}
